<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
  import { ref } from 'vue'
  import { patientListApi, removePatientApi } from '@/apis/patient'
  import { onShow } from '@dcloudio/uni-app'
  import { useConsultStore } from '@/stores/consult'
  import type { PatientItem } from '@/types/patient'

  // 适配问诊选择
  const props = defineProps<{ type: 'consult' | 'archive' }>()
  const store = useConsultStore()
  const selPatient = (id: string) => {
    if (props.type) {
      store.patientId = id
    }
  }
  const swipeOptions = ref([
    {
      text: '删除',
      style: {
        backgroundColor: '#dd524d',
        color: '',
        fontSize: '',
      },
    },
  ])

  // 获取列表数据
  const patinetList = ref<PatientItem[]>([])
  const getPatientList = async () => {
    const res = await patientListApi()
    patinetList.value = res
  }
  onShow(() => {
    getPatientList()
  })

  // 滑动操作删除患者点击
  const onSwipeActionClick = async (id: string, index: number) => {
    // 传递数据的 id 值和索引值
    console.log(id, index)
    uni.showModal({
      title: '提示',
      content: '您确定要删除吗?',
      success: async (success) => {
        if (success.confirm) {
          await removePatientApi(id)
          // Vue 实例中的数据也要同步删除
          patinetList.value.splice(index, 1)
          // 删除后，当前选中的 id 值也同步删除
          if (store.patientId === id) store.patientId = ''
          uni.showToast({
            title: '删除成功',
            icon: 'success',
            mask: true,
          })
        }
      },
    })
  }
  // 下一步:选择患者
  const go = () => {
    if (!store.patientId) return uni.showToast({ title: '请选择患者！' })
    uni.navigateTo({ url: '/subpkg_consult/payment/payment' })
  }
</script>

<template>
  <scroll-page>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <uni-swipe-action-item
          :right-options="swipeOptions"
          v-for="(item, index) in patinetList"
          :key="item.id"
          @click="onSwipeActionClick(item.id, index)"
        >
          <view
            class="archive-card"
            @click="selPatient(item.id)"
            :class="{ active: store.patientId === item.id }"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">{{
                item.idCard.replace(/^(.{6}).+(.{4})$/, '$1********$2')
              }}</text>
              <text v-if="item.defaultFlag === 1" class="default">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.genderValue }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/form?id=${item.id}`"
            >
              <uni-icons
                type="icon-edit"
                size="20"
                color="#16C2A3"
                custom-prefix="iconfont"
                :url="`/subpkg_archive/form/form?id=${item.id}`"
              />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patinetList.length < 6" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/form/form">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
    </view>
    <!-- 选择模式显示 -->
    <view v-if="type">
      <button @click="go" class="uni-button">下一步</button>
    </view>
  </scroll-page>
</template>

<style lang="scss">
  @import './index.scss';
</style>
